package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dao.MonthRepository;
import com.example.entity.Month;
import com.example.service.MonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class MonthServiceImpl implements MonthService {
    @Autowired
    MonthRepository monthRepository;
    @Override
    public JSONObject insert(Month m) {
        JSONObject jsonObject = new JSONObject();
        Month month_temp = monthRepository.findByYearAndMonth(m.getYear(), m.getMonth());
        if (month_temp == null) {
            String uuid = UUID.randomUUID().toString();
            Month month = monthRepository.save(new Month(m.getYear(), m.getMonth(), uuid, null));
            month.setHasChild(false);
            month.setHasParent(false);
            jsonObject.put("msg", "success");
            jsonObject.put("data", month);
            return jsonObject;
        }
        Long id = month_temp.getId();
        Optional<Month> month_Optional = monthRepository.findById(id);
        Month month = month_Optional.get();
        jsonObject.put("msg", "已经重复请勿,重复添加");
        jsonObject.put("data", month);
        return jsonObject;
    }

    @Override
    public boolean deleteById(Long id) {
        Optional<Month> month_Optional =  monthRepository.findById(id);
        Month month = month_Optional.get();
        if (month.getEventList() == null || month.getEventList().size() == 0) {
            monthRepository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public List<JSONObject> getTimeLine() {
        List<JSONObject> yearObjList = new LinkedList<>();
        List<Month> monthNodeList = monthRepository.findAll();
        HashMap<Integer, List<Month>> group = new HashMap<>();
        for (Month monthNode:monthNodeList) {
            int cur_year = Integer.valueOf(monthNode.getYear());
            if (group.containsKey(cur_year)) {
                group.get(cur_year).add(monthNode);
            }else {
                List<Month> months = new LinkedList<>();
                months.add(monthNode);
                group.put(cur_year, months);
            }
        }

        Collection<Integer> keySet = group.keySet();
        List<Integer> keyList = new ArrayList<Integer>(keySet);
        Collections.sort(keyList);
        for (Integer key:keyList) {
            JSONObject yearObj = new JSONObject();
            yearObj.put("year", key);
            List<Month> monthList = group.get(key);
            List<JSONObject> monthObjList = new LinkedList<>();
            for (Month m :monthList) {
                JSONObject monthObj = new JSONObject();
                monthObj.put("month", m.getMonth());
                monthObj.put("id", m.getId());
                monthObjList.add(monthObj);
            }
            yearObj.put("children", monthObjList);
            yearObjList.add(yearObj);
        }

        return yearObjList;
    }

    @Override
    public Month selectById(Long id) {
        Optional<Month> month_Optional =  monthRepository.findById(id);
        Month monthNode = month_Optional.get();

        return monthNode;
    }

    @Override
    public Month findByYearAndMonth(String year, String month) {
        Month month_temp = monthRepository.findByYearAndMonth(year, month);
        if (month_temp == null) {
            return null;
        }
        Optional<Month> month_Optional =  monthRepository.findById(month_temp.getId());
        Month monthNode = month_Optional.get();
        return monthNode;
    }

    @Override
    public JSONObject selectAll() {
        JSONObject jsonObject = new JSONObject();
        List<Month> monthList = monthRepository.findAll();
        jsonObject.put("msg", "success");
        jsonObject.put("data", monthList);
        return jsonObject;
    }

    @Override
    public JSONObject update(Month month) {
        JSONObject jsonObject = new JSONObject();
        Optional<Month> monthOptional = monthRepository.findById(month.getId());
        Month monthNode = monthOptional.get();
        monthNode.setYear(month.getYear());
        monthNode.setMonth(month.getMonth());
        Month newMonthNode = monthRepository.save(monthNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", newMonthNode);
        return jsonObject;
    }

    @Override
    public JSONObject deleteById(Month month) {
        JSONObject jsonObject = new JSONObject();
        Optional<Month> monthOptional = monthRepository.findById(month.getId());
        Month monthNode = monthOptional.get();
        monthRepository.delete(monthNode);
        jsonObject.put("msg", "success");
        jsonObject.put("data", monthNode);
        return jsonObject;
        // 这个地方判断可以改为isConnected
//        if (monthNode.getEventList() == null || monthNode.getEventList().size() == 0) {
//            monthRepository.delete(monthNode);
//            jsonObject.put("msg", "success");
//            jsonObject.put("data", monthNode);
//            return jsonObject;
//        }
//        jsonObject.put("msg", "删除失败，因为还有关系未删除");
//        return jsonObject;
    }
}
